package command
{
	import com.adobe.cairngorm.control.CairngormEvent;
	
	import flash.net.URLRequestMethod;
	
	import model.ApplicationModel;
	
	import mx.controls.Alert;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;
	import mx.rpc.http.HTTPService;
	
	import vo.QuestionAnswer;
	
	public class SaveStatsCommand extends BaseCommand
	{
		
		private var _model:ApplicationModel = ApplicationModel.instance;
		public function SaveStatsCommand()
		{
		}

		public override function doExecute(event:CairngormEvent):void
		{
			//QuestionAnswer questionResult
			var params:Object = new Object();
			var xSolution:String = "";
		    for each ( var result : QuestionAnswer in _model.questionsResolved)
			{	
				 xSolution = xSolution + result.id+";"+result.solutionSelected+"&";
			}

			params.userID = _model.userID;
			params.levelID = _model.level;
			params.stats = xSolution;
			// for futures
			// params.questions = xSolution;
			 var httpService:HTTPService = new HTTPService();
			 httpService.url= _model.URL_SAVE_STAT;
			 httpService.useProxy = false;
			 httpService.addEventListener(ResultEvent.RESULT,onResult);
          	 httpService.addEventListener(FaultEvent.FAULT, onFault);
          	 httpService.method = URLRequestMethod.POST;
          	 httpService.send(params);	
 
		 
		}
		public function onResult(data:Object):void
		{
			
			
		}
		public function onFault(data:Object):void
		{
			Alert.show("Fail","Error al salvar las estadisticas");
		}
	}
}